<template>
  <div class="fetch-number-kiosk background-image">
    <!-- <div class="SonButtomBox">
      <div class="imgBox">
        <img src="../../../assets/images/ssreport/hushi.png" alt="" class="no-pointer-event" />
      </div>
    </div> -->
    <!-- 顶部信息栏 -->
    <div class="header">
      <div class="hospital-info">
        <h1>{{ hospitalName }}</h1>
        <div class="datetime">{{ currentTime }}</div>
      </div>
      <div class="language-switcher">
        <button @click="switchLanguage('zh')">中文</button>
        <button @click="switchLanguage('en')">English</button>
      </div>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
      <div class="service-selection" v-if="!ticketInfo">
        <h2>请选择服务类型</h2>
        <div class="service-grid">
          <button v-for="service in services" :key="service.id" @click="selectService(service)" class="service-btn">
            {{ service.name }}
            <span v-if="service.waitNumber">当前等待: {{ service.waitNumber }}人</span>
          </button>
        </div>
      </div>

      <!-- 身份验证 -->
      <div class="identity-verification" v-if="selectedService && !ticketInfo">
        <h2>{{ selectedService.name }}</h2>

        <div class="input-group">
          <label>请输入就诊卡号/身份证号:</label>
          <input type="text" v-model="patientId" placeholder="请刷卡或输入号码" ref="idInput" @keyup.enter="fetchNumber">
        </div>

        <div class="patient-info" v-if="patientName">
          <p>姓名: {{ patientName }}</p>
          <p>待取项目: {{ selectedService.name }}</p>
        </div>

        <div class="action-buttons">
          <button @click="selectedService = null" class="back-btn">返回</button>
          <button @click="fetchNumber" :disabled="!patientId" class="confirm-btn">
            取号
          </button>
        </div>
      </div>

      <!-- 取号结果 -->
      <div class="result-screen" v-if="ticketInfo">
        <div class="ticket-card">
          <h2>取号成功</h2>
          <div class="ticket-number">号码: {{ ticketInfo.number }}</div>
          <div class="service-type">服务类型: {{ ticketInfo.serviceName }}</div>
          <div class="patient-name">姓名: {{ ticketInfo.patientName }}</div>
          <div class="wait-info">
            当前等待人数: {{ ticketInfo.waitCount }}人<br>
            预计等待时间: {{ ticketInfo.estimatedWait }}分钟
          </div>
          <div class="datetime">取号时间: {{ currentTime }}</div>
        </div>

        <button @click="resetProcess" class="new-ticket-btn">继续取号</button>

        <div class="print-status" v-if="printing">
          <span class="printing">正在打印取号单...</span>
        </div>
      </div>
    </div>

    <!-- 底部操作栏 -->
    <div class="footer">
      <button @click="showHelp" class="help-btn">帮助</button>
      <button @click="callStaff" class="staff-call-btn">呼叫工作人员</button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import axios from 'axios';

export default {
  setup() {
    // 医院信息
    const hospitalName = ref('XX市人民医院')
    const currentTime = ref('')

    // 服务类型
    const services = ref([
      { id: 1, name: '采血取号', waitNumber: 8 },
      { id: 2, name: '采尿取号', waitNumber: 12 },
      // { id: 3, name: '放射科取片', waitNumber: 5 },
      // { id: 4, name: '报告领取', waitNumber: 3 }
    ])
    const selectedService = ref(null)

    // 患者信息
    const patientId = ref('')
    const patientName = ref('')

    // 票据信息
    const ticketInfo = ref(null)
    const printing = ref(false)

    // 更新当前时间
    const updateTime = () => {
      const now = new Date()
      currentTime.value = now.toLocaleTimeString('zh-CN', {
        hour12: false,
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      })
    }

    // 选择服务类型
    const selectService = (service) => {
      selectedService.value = service
      // 自动聚焦到输入框
      setTimeout(() => {
        const input = document.querySelector('#idInput')
        if (input) input.focus()
      }, 100)
    }

    // 模拟患者信息查询
    const queryPatientInfo = (id) => {
      // 实际项目中这里会调用API
      const ss = id.substring(id.length - 1, id.length);
      console.log("caonima" + ss);
      const patients = [
        { name: '杨*维' },
        { name: '胡*东' },
        { name: '李*振' },
        { name: '王*浩' },
        { name: '宋*宪' },
        { name: '邱*栋' },
        { name: '庞*贝' },
        { name: '高*亨' },
        { name: '周*娜' },
        { name: '陈*山' }
      ]
      return patients[ss] || { name: '未知患者' }
    }

    // 取号
    const fetchNumber = () => {
      if (!patientId.value) return

      // 查询患者信息
      const patient = queryPatientInfo(patientId.value)
      patientName.value = patient.name

      // 模拟取号过程
      printing.value = false;

      // 模拟API调用延迟
      axios.get(window.apiUrl.VITE_SERVER_URL + '/callNum/business/callNum', { params: { userName: patientName.value }, headers: { 'Authorization': 'Bearer ' + localStorage.getItem('token') } })
        .then(response => {
          ;
          ticketInfo.value = {
            number: response.data.data.callNum,
            serviceName: response.data.data.winName,
            patientName: response.data.data.userName,
            waitCount: response.data.data.linePeople,
            estimatedWait: response.data.data.expectTime
          }
        })
        .catch(error => {
          console.error('There was an error!', error);
        });
      // setTimeout(() => {
      //   ticketInfo.value = {
      //     number: selectedService.value.id + '-' + Math.floor(Math.random() * 1000),
      //     serviceName: selectedService.value.name,
      //     patientName: patient.name,
      //     waitCount: selectedService.value.waitNumber,
      //     estimatedWait: Math.floor(selectedService.value.waitNumber * 1.5)
      //   }

      //   // 模拟打印延迟
      //   setTimeout(() => {
      //     printing.value = false
      //   }, 1500)
      // }, 1000)
    }

    // 重置流程
    const resetProcess = () => {
      selectedService.value = null
      patientId.value = ''
      patientName.value = ''
      ticketInfo.value = null
    }

    // 帮助功能
    const showHelp = () => {
      alert('如需帮助，请按呼叫工作人员按钮')
    }

    // 呼叫工作人员
    const callStaff = () => {
      alert('工作人员已收到呼叫，请稍候...')
    }

    // 切换语言
    const switchLanguage = (lang) => {
      alert(`切换语言到: ${lang === 'zh' ? '中文' : 'English'}`)
      // 实际项目中这里会切换i18n语言
    }

    // 初始化
    onMounted(() => {
      updateTime()
      const timer = setInterval(updateTime, 1000)

      onUnmounted(() => {
        clearInterval(timer)
      })
    })

    return {
      hospitalName,
      currentTime,
      services,
      selectedService,
      patientId,
      patientName,
      ticketInfo,
      printing,
      selectService,
      fetchNumber,
      resetProcess,
      showHelp,
      callStaff,
      switchLanguage
    }
  }
}
</script>

<style scoped>
.fetch-number-kiosk {
  width: 96vw;
  height: 94vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
  font-family: 'Microsoft YaHei', sans-serif;
  overflow: hidden;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  background-color: #1e88e5;
  color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.hospital-info h1 {
  margin: 0;
  font-size: 28px;
}

.datetime {
  font-size: 18px;
  margin-top: 5px;
}

.language-switcher button {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 15px;
  margin-left: 10px;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.main-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.service-selection,
.identity-verification,
.result-screen {
  width: 90%;
  max-width: 800px;
  background-color: white;
  border-radius: 10px;
  padding: 30px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.service-selection h2,
.identity-verification h2,
.result-screen h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}

.service-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.service-btn {
  padding: 30px 15px;
  background-color: #2196f3;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.service-btn:hover {
  background-color: #0d8bf2;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.service-btn span {
  font-size: 16px;
  margin-top: 8px;
  opacity: 0.9;
}

.input-group {
  margin-bottom: 25px;
}

.input-group label {
  display: block;
  margin-bottom: 10px;
  font-size: 18px;
  color: #555;
}

.input-group input {
  width: 96%;
  padding: 15px;
  font-size: 20px;
  border: 2px solid #ddd;
  border-radius: 8px;
  text-align: center;
}

.patient-info {
  background-color: #f5f5f5;
  padding: 15px;
  border-radius: 8px;
  margin: 25px 0;
  font-size: 18px;
}

.action-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

.back-btn,
.confirm-btn {
  padding: 15px 30px;
  font-size: 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  min-width: 150px;
}

.back-btn {
  background-color: #e0e0e0;
  color: #333;
}

.confirm-btn {
  background-color: #4caf50;
  color: white;
}

.confirm-btn:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.ticket-card {
  background-color: #f9f9f9;
  border: 2px dashed #1e88e5;
  padding: 30px;
  text-align: center;
  margin-bottom: 30px;
  border-radius: 10px;
}

.ticket-number {
  font-size: 32px;
  font-weight: bold;
  color: #e91e63;
  margin: 20px 0;
}

.service-type,
.patient-name,
.wait-info {
  font-size: 20px;
  margin: 15px 0;
}

.wait-info {
  line-height: 1.6;
}

.new-ticket-btn {
  width: 100%;
  padding: 20px;
  background-color: #1e88e5;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 20px;
  cursor: pointer;
  margin-top: 20px;
}

.print-status {
  margin-top: 20px;
  text-align: center;
  font-size: 16px;
  color: #ff9800;
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 15px 30px;
  background-color: #f0f2f5;
}

.help-btn,
.staff-call-btn {
  padding: 15px 30px;
  font-size: 18px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  min-width: 200px;
}

.help-btn {
  background-color: #ffc107;
  color: #333;
}

.staff-call-btn {
  background-color: #f44336;
  color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .service-grid {
    grid-template-columns: 1fr;
  }

  .header {
    flex-direction: column;
    text-align: center;
  }

  .language-switcher {
    margin-top: 10px;
  }

  .back-btn,
  .confirm-btn,
  .help-btn,
  .staff-call-btn {
    min-width: 120px;
    padding: 12px 20px;
  }
}

.background-image {
  background-image: url('@/assets/images/ssreport/bj.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
}
</style>